#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
读取HTML文件进行解析
"""
import os
from lxml import etree

def main():
    """
    测试
    :return:
    """
    # print(os.path.abspath(__file__))  # 显示当前文件的地址
    # print(os.getcwd())  # 显示主程序入口的地址

    # 指定解析器HTMLParser，会根据文件修复HTML文件中缺失的如声明信息
    html = etree.parse(source='test.html', parser=etree.HTMLParser())
    result = etree.tostring(html)
    print(type(html))
    print(type(result))
    print("result=", result)

    print("============================================")
    # 获取所有节点
    result = html.xpath("//*")  # //代表获取子孙节点，*代表获取所有
    print(type(html))
    print(type(result))
    print("result=", result)

    print("============================================")
    # 获取li节点
    result = html.xpath("//li")
    print(type(html))
    print(type(result))
    print("result=", result)

    print("============================================")
    # 获取子节点a
    result = html.xpath("//li/a")   # 选择所有li节点的所有直接a节点
    print(type(result))
    print("result=", result)

if __name__ == '__main__':
    main()